package com.gitee.hermer.engine.jars.core.context.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;
import org.springframework.data.annotation.Transient;

import com.gitee.hermer.boot.jee.commons.collection.CollectionUtils;
import com.gitee.hermer.boot.jee.commons.collection.StringCache;
import com.gitee.hermer.boot.jee.commons.exception.ErrorCode;
import com.gitee.hermer.boot.jee.commons.reflect.ReflectUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gitee.hermer.boot.jee.commons.bean.utils.BeanLocator;
import com.gitee.hermer.boot.jee.commons.utils.DateUtils;
import com.gitee.hermer.boot.jee.commons.verify.Assert;
import com.gitee.hermer.engine.jars.core.app.domain.EngineAppContext;
import com.gitee.hermer.engine.jars.core.context.IEngineAppContext;
import com.gitee.hermer.engine.jars.core.context.impl.EngineAppContextImpl.EngineAppLoaderState;
import com.gitee.hermer.engine.jars.core.domain.ToStringObject;


/**
 * 模块中APP启动后实例
 * @author hermer
 *
 */
public class EngineAppInfoContext extends ToStringObject implements Serializable{

	@Transient
	@JsonIgnore
	private transient EngineAppContext jobContext;
	private String cacheKey;
	private String jarFileName;
	private String fileMd5;
	@Transient
	@JsonIgnore
	private transient ClassLoader classLoader;
	@Transient
	@JsonIgnore
	private transient Runnable destroy;

	private String lastUpdateDate;
	private String lastRestartDate;

	@Override
	public boolean equals(Object that) {
		return getCacheKey().equals(((EngineAppInfoContext)that).getCacheKey());
	}

	public EngineAppLoaderState getLoaderState() {
		Assert.hasText(cacheKey);
		try{
			EngineAppLoaderState state = BeanLocator.getBean(IEngineAppContext.class).getCurrentLoaderState(cacheKey);
			if(state != null)
				return state;
		}catch (Exception e) { }
		return EngineAppLoaderState.DEFAULT;
	}
	public EngineAppInfoContext(String cacheKey){
		this.cacheKey = cacheKey;
	}

	public EngineAppInfoContext(String cacheKey,String jarFileName,String fileMd5,String lastUpdateDate){
		this.cacheKey = cacheKey;
		this.jarFileName = jarFileName;
		this.fileMd5 = fileMd5;
		this.lastUpdateDate = lastUpdateDate;
	}

	public EngineAppInfoContext(String cacheKey,String jarFileName,String fileMd5,String lastUpdateDate,ApplicationContext applicationContext,
			ApplicationContext parent,Runnable destroy){
		this.cacheKey = cacheKey;
		this.jarFileName = jarFileName;
		this.fileMd5 = fileMd5;
		this.lastUpdateDate = lastUpdateDate;
		this.lastRestartDate = DateUtils.getCurrentTime();
		this.destroy = destroy;
		this.classLoader = applicationContext.getClassLoader();
		jobContext = applicationContext.getBean(EngineAppContext.class);
		jobContext.setContext(applicationContext);
		jobContext.setParentContext(parent);
	}

	public EngineAppContext getJobContext() {
		return jobContext;
	}

	public void setJobContext(EngineAppContext jobContext) {
		this.jobContext = jobContext;
	}

	public String getCacheKey() {
		return cacheKey;
	}

	public void setCacheKey(String cacheKey) {
		this.cacheKey = cacheKey;
	}

	public String getJarFileName() {
		return jarFileName;
	}


	public void setJarFileName(String jarFileName) {
		this.jarFileName = jarFileName;
	}


	public String getFileMd5() {
		return fileMd5;
	}


	public void setFileMd5(String fileMd5) {
		this.fileMd5 = fileMd5;
	}


	public ClassLoader getClassLoader() {
		return classLoader;
	}


	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}


	public Runnable getDestroy() {
		return destroy;
	}


	public void setDestroy(Runnable destroy) {
		this.destroy = destroy;
	}


	public String getLastUpdateDate() {
		return lastUpdateDate;
	}


	public void setLastUpdateDate(String lastUpdateDate) {
		this.lastUpdateDate = lastUpdateDate;
	}


	public String getLastRestartDate() {
		return lastRestartDate;
	}


	public void setLastRestartDate(String lastRestartDate) {
		this.lastRestartDate = lastRestartDate;
	}

	@Override
	public <T> T getBean(Class<T> c) throws Exception {
		return jobContext.getBean(c);
	}

	public <T> T getBean(String beanName) throws Exception{
		return jobContext.getBean(beanName);
	}

}
